package com.vf.cloud.scene.mvc.biz.service.impl;

import java.util.Date;

import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.common.util.R;
import com.vf.cloud.common.util.UuidUtil;
import com.vf.cloud.pass.common.repository.base.Thumbnail;
import com.vf.cloud.pass.mvc.biz.service.IThumbnail;
import com.vf.cloud.scene.common.domain.SubLayer;
import com.vf.cloud.scene.common.domain.SubLayerStylePoi;
import com.vf.cloud.scene.common.domain.SubStyleTemplatePoi;
import com.vf.cloud.scene.mvc.biz.service.ILayerService;

@Service
public class LayerService implements ILayerService {

	@Autowired
	private IThumbnail thumbnailImpl;

	@Override
	public R<Page<SubLayer>> findList(String projectId, String keywords, int cur, int limit) {
		String sqlExceptSelect = " FROM " + SubLayer.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(projectId)) {
			sqlExceptSelect += " AND O.PROJECT_ID = '" + projectId + "' ";
		}
		sqlExceptSelect += "  ORDER BY   O.SORT asc ";
		Page<SubLayer> pages = SubLayer.dao.paginate(cur, limit, "select * ", sqlExceptSelect);
		return R.ok(pages);
	}

	@Override
	public R<Object> addPoiStyle(String templateId, String layerId) {
		try {
			SubStyleTemplatePoi styleTemplatePoi = SubStyleTemplatePoi.dao.findById(templateId);
			if (styleTemplatePoi == null)
				return R.failed("POI模板不存在！");
			SubLayer layer = SubLayer.dao.findById(layerId);
			if (layer == null)
				return R.failed("图层不存在!");

			SubLayerStylePoi layerStylePoi = JSONObject
					.toJavaObject(JSONObject.parseObject(JsonKit.toJson(styleTemplatePoi)), SubLayerStylePoi.class);
			if (layerStylePoi != null) {
				layerStylePoi.setId(layerId);
				if (layerStylePoi.save()) {
					Thumbnail thumbnailN = thumbnailImpl.findById(templateId + "_normal");
					if (thumbnailN != null) {
						Thumbnail normalEntity = new Thumbnail();
						normalEntity.setSize(thumbnailN.getSize());
						normalEntity.setName(thumbnailN.getName());
						normalEntity.setContentType(thumbnailN.getContentType());
						normalEntity.setUploadDate(thumbnailN.getUploadDate());
						normalEntity.setContent(thumbnailN.getContent());
						normalEntity.setId(layerId + "_normal");
						thumbnailImpl.save(normalEntity);
					}

					Thumbnail thumbnailA = thumbnailImpl.findById(templateId + "_activate");
					if (thumbnailA != null) {
						Thumbnail activateEntity = new Thumbnail();
						activateEntity.setSize(thumbnailA.getSize());
						activateEntity.setName(thumbnailA.getName());
						activateEntity.setContentType(thumbnailA.getContentType());
						activateEntity.setUploadDate(thumbnailA.getUploadDate());
						activateEntity.setContent(thumbnailA.getContent());
						activateEntity.setId(layerId + "_activate");
						thumbnailImpl.save(activateEntity);
					}
					return R.ok("创建成功！", layerStylePoi);
				} else {
					return R.ok("创建失败！");
				}
			}
			return R.ok("创建失败！");
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Override
	public R<SubLayerStylePoi> savePoiStyle(MultipartFile file1, MultipartFile file2, SubLayerStylePoi model) {
		try {
			Thumbnail normalEntity = null;
			Thumbnail activateEntity = null;
			if (file1 != null) {
				normalEntity = new Thumbnail();
				normalEntity.setSize(file1.getSize());
				normalEntity.setName(model.getLabelNormalText());
				normalEntity.setContentType(file1.getContentType());
				normalEntity.setUploadDate(new Date(System.currentTimeMillis()));
				normalEntity.setContent(new Binary(file1.getBytes()));
			}
			if (file2 != null) {
				activateEntity = new Thumbnail();
				activateEntity.setSize(file2.getSize());
				activateEntity.setName(model.getLabelNormalText());
				activateEntity.setContentType(file2.getContentType());
				activateEntity.setUploadDate(new Date(System.currentTimeMillis()));
				activateEntity.setContent(new Binary(file2.getBytes()));
			}
			if (StrKit.notBlank(model.getId())) {
				if (model.update()) {
					try {
						if (normalEntity != null) {
							normalEntity.setId(model.getId() + "_normal");
							thumbnailImpl.save(normalEntity);
						}
						if (activateEntity != null) {
							activateEntity.setId(model.getId() + "_activate");
							thumbnailImpl.save(activateEntity);
						}
					} catch (Exception e) {
					}
					return R.ok(model);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				model.setId(UuidUtil.getUUID());
				if (model.save()) {
					try {
						if (normalEntity != null) {
							normalEntity.setId(model.getId() + "_normal");
							thumbnailImpl.save(normalEntity);
						}
						if (activateEntity != null) {
							activateEntity.setId(model.getId() + "_activate");
							thumbnailImpl.save(activateEntity);
						}
					} catch (Exception e) {
					}
					return R.ok(model);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}